﻿using System.Reflection;
using System.Text;

/*
 * Jonathan
 * Created by: Jonathan Montiverdi
 * Date: 6/13/2012
 * License: http://jonathan.codeplex.com/license
 */
namespace Jonathan.Data
{
   /// <summary>
   /// Common data for Jonathan.
   /// </summary>
   public static class Common
   {
      private static string s_ApplicationPath;
      
      /// <summary>
      /// The relative path to the data directory.
      /// </summary>
      public const string DATA_DIR = ".\\Data\\";
      
      /// <summary>
      /// The main database name.
      /// </summary>
      public const string MAIN_DB = "Main.db";
      
      /// <summary>
      /// The definition database name.
      /// </summary>
      public const string DEF_DB_NAME = "Definitions";
      
      /// <summary>
      /// The definition database file name.
      /// </summary>
      public const string DEF_DB = DEF_DB_NAME + ".db";
      
      /// <summary>
      /// The definition database path.
      /// </summary>
      public const string DEF_DB_PATH = DATA_DIR + DEF_DB;
      
      /// <summary>
      /// The main database name.
      /// </summary>
      public const string OPTIONS_FILE = "user_data.db";
      
      /// <summary>
      /// The relative path to the main database.
      /// </summary>
      public const string MAIN_DB_PATH = DATA_DIR + MAIN_DB;
      
      /// <summary>
      /// The url to the seek deep within wiki.
      /// </summary>
      public const string WIKI_URL = "http://learnto.seekdeepwithin.com/";
      
      /// <summary>
      /// The url to the seek deep within wiki download area.
      /// </summary>
      public const string WIKI_DL_URL = WIKI_URL + "downloads/";
      
      /// <summary>
      /// The path to the registry folder for Jonathan.
      /// </summary>
      public const string REGISTRY_PATH = "SOFTWARE\\SeekDeepWithin\\Jonathan";
      
      /// <summary>
      /// Gets the application path.
      /// </summary>
      public static string ApplicationPath
      {
         get
         {
            return s_ApplicationPath ??
               (s_ApplicationPath = Assembly.GetExecutingAssembly ().Location.Replace ("Jonathan.exe", ""));
         }
      }
      
      /// <summary>
      /// Gets the head section any html document in jonathan.
      /// </summary>
      /// <param name="title">Title of document.</param>
      /// <returns>The head section html.</returns>
      public static string GetHtmlHead (string title)
      {
         var html = new StringBuilder ();
         
         html.AppendLine ("<head>");
         html.AppendLine (string.Format ("<title>Jonathan - {0}</title>", title));
         html.AppendLine ("<script src='styles/js/jQuery.js'></script>");
         
         html.AppendLine ("<link rel='StyleSheet' type='text/css' href='styles/css/bootstrap.min.css' />");
         html.AppendLine ("<link rel='StyleSheet' type='text/css' href='styles/css/defaultBlue.css' />");
         html.AppendLine ("<link rel='StyleSheet' type='text/css' href='styles/css/treeview.css' />");
         AdditionalHeaderContent (html, title);
         html.AppendLine ("</head>");
         
         return html.ToString ();
      }
      
      /// <summary>
      /// Builds the addtional header content.
      /// </summary>
      /// <param name="html">String to append html.</param>
      /// <param name="title">Title of page</param>
      private static void AdditionalHeaderContent (StringBuilder html, string title)
      {
         html.AppendLine ("<script type=\"text/javascript\">");
         html.AppendLine ("$(document).ready(function () {resizeContent ();});");
         html.AppendLine ("$(window).resize (function (e) {resizeContent ();});");
         html.AppendLine ("function resizeContent ()");
         html.AppendLine ("{");
         if (title == "Books")
         {
            html.AppendLine ("var w = $(document).width ();");
            html.AppendLine ("var h = $(document).height ();");
            html.AppendLine ("$('#downloadsTitle').width (w);");
            html.AppendLine ("$('#downloadContent').width (w);");
            html.AppendLine ("$('#downloadsTitle').offset ({top: h - 225, left: 0});");
            html.AppendLine ("$('#downloadContent').offset ({top: h - 200, left: 0});");
         }
         else
         {
            html.AppendLine ("var w = $(document).width () - 20;");
            html.AppendLine ("var h = $(document).height () - 50;");
            html.AppendLine ("$('#header').width (w);");
            html.AppendLine ("$('#mainContent').width (w - 193);");
            html.AppendLine ("$('#mainContentTitle').width (w - 201);");
            html.AppendLine ("$('#mainContent').height (h - 28);");
            html.AppendLine ("$('#contentsTree').height (h - 36);");
         }
         html.AppendLine ("}");
         html.AppendLine ("</script>");
      }
   }
}
